# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.
import  os
from time import sleep

import uiautomator2 as u2
from pyminitouch import safe_connection, safe_device, MNTDevice, CommandBuilder

def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.

def getAllConnectedDeviceList():
    s = os.popen("adb devices")
    a = s.read()
    list = a.split('\n')
    deviceIdList = []
    for temp in list:
        print("yzprt temp:", temp)
        if len(temp.split()) > 1:
            if temp.split()[1] == 'device':
                deviceIdList.append(temp.split()[0])
    print('本次共扫描出%s个安卓设备' % len(deviceIdList))
    deviceList = []
    for ee in deviceIdList:
        d = u2.connect(ee)
        print("yzprt d.info:", d.info)
        print("yzprt d.serial:", d.serial)
        deviceList.append(d)
    return deviceList
def multiClick(deviceList:list, x:float=None, y:float=None, selStrr:str=None):
    for i in range(len(deviceList)):
        d:u2.Device = deviceList[i]
        if selStrr==None:
            d.click(x,y)
        else:
            d(text=selStrr).click()
def wFunc(d:u2.Device):
    print("haha")
    print(d.info)
# def clickk(d:u2s.Device,xPathWatcher:u2s.watcher.XPathWatcher):
#     print("yzprt entered CLICKK:")
#     xPathWatcher.click()
#     d.watcher.remove()
#     print("yzprt text: %s is removed:"%(text))
# def whenClick(d:u2s.Device,text:str):
#     pass
#     # d.watcher.when(text).call(lambda d,text:clickk(d,text))
#
#     d.watcher.when(text).call(lambda d:clickk(d,d.watcher.when(text)))
# def click3(d:u2s.Device):
#     d.watcher.remove("小红书助手")
#     d.watcher.start()
#     d.watcher.start()
#     print("yzprt click3:")
def setWatchers(d:u2.Device,watchList:list):
    for i in range(len(watchList)):
        # d.watcher.when(watchList[i]).call(lambda d: click_callback(d,watchList))
        xPathWatcher = d.watcher.when(watchList[i])
        xPathWatcher.call(lambda  d: updateWatchers(d,watchList,xPathWatcher))

def updateWatchers(d:u2.Device,watchList:list,xPathWatcher:u2.watcher.XPathWatcher):
    print("yzprt 11:", 11)
    text = watchList.pop(0)
    d(text=text).click()
    # xPathWatcher.click()
    print("yzprt 22:", 22)
    d.watcher.reset()
    print("yzprt 33:", 33)
    print("yzprt 44:", 44)
    setWatchers(d,watchList)
    print("yzprt 55:", 55)
    d.watcher.start()
    print("yzprt 99:", 99)
# listt = ['小红书助手', '打开辅助服务', 'CodeBoy小红书助手']
# 回调说明
def click_callback(d: u2.Device,listt):
    text = listt.pop(0)
    print("yzprt text:", text)
    print("yzprt listt:", listt)
    d(text=text).click()
    d.watcher.reset()
    setWatchers(d,listt)
    d.watcher.start()
    print("yzprt 99:", 99)
def watchAndSyn(deviceList:list):
    d:u2.Device = deviceList[0]

    # d.watcher.when('小红书助手').click()
    # d.watcher.when('小红书助手').call(lambda d:click3(d))
    # whenClick(d,"小红书助手")
    # d.watcher.when("打开辅助服务").click()
    # d.watcher.when("CodeBoy小红书助手").click()
    # d.watcher.when("//*[resourceId='android:id/switch_widget']").click()
    # d.watcher.when("确定").click()
    setWatchers(d,['小红书助手', '打开辅助服务', 'CodeBoy小红书助手'])


    d.watcher.start()

    sleep(60)

if __name__ == '__main__':

    deviceList = getAllConnectedDeviceList()
    # multiClick(deviceList,x=0.155,y=0.100)
    # sleep(3)
    # multiClick(deviceList,x=0.770,y=0.680)
    # multiClick(deviceList,selStrr="小红书助手")
    # sleep(3)
    # multiClick(deviceList,selStrr="打开辅助服务")
    watchAndSyn(deviceList)
    print_hi('PyCharm')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/

    # os.system("adb shell /system/bin/screencap -p /sdcard/screen.png")  # 截取屏幕，图片命名为screen.png
    # os.system("adb shell input tap 450 450")
    # os.system("adb shell input swipe 450 950 450 250 100")
    # d = u2s.connect("4072c433")  # connect to device
    # print("yzprt type(d):", type(d))
    # print(d.info)
    # d.app_start("com.xingin.xhs")
    # d.implicitly_wait(10.0)  # 也可以通过d.settings['wait_timeout'] = 10.0 修改
    # d(text="编辑资料").click()  # if Settings button not show in 10s, UiObjectNotFoundError will raised
    # print("wait timeout", d.implicitly_wait())  # get default implicit wait
    # d(text="显示与亮度").click()
    # d.screen_off()
    # d.screen_on()
    # d.toast.show("Hello world", 3.0)

    # _DEVICE_ID = '4072c433'
    #
    # device = MNTDevice(_DEVICE_ID)
    #
    # # print the maximum x and Y coordinates
    # print("max x:", device.connection.max_x)
    # print("max y:", device.connection.max_y)
